import java.net._
import java.io._
import scala.io._
import scala.collection.mutable._
import scala.actors.Futures._
import scala.concurrent._

object Client {
    def main(args: Array[String]) {
      
      val clientSocket = new Socket("localhost",6787)
      val objectOut = new ObjectOutputStream(clientSocket.getOutputStream())
      val objectIn = new ObjectInputStream(clientSocket.getInputStream())
      val inUser = new BufferedReader( new InputStreamReader(System.in))
      
      var clientArrayBuffer = new ArrayBuffer[Int]
      var outSentence = ""
      var lock = new Lock
      
      future {
        while(true) {
          //lock.acquire
          var objectFromServer=objectIn.readObject()
          println("counter comes here")
          //lock.release
          objectFromServer match {
            case clientArrayBufferClass(cAB) =>
              println("Har mottat en ClientArrayBuffer")
              println(cAB)
            case StringCarrier(messageString, actorIDFrom, actorIDTo) =>
              println("New message from user " + actorIDFrom + ": " + messageString)
          }
        }
      }
      
      while(true) {
        Thread.sleep(5000)
        outSentence = "melding"
        //lock.acquire
        println("Skriver ny melding til server")
        objectOut.writeObject(new StringCarrier(outSentence,-2, 0))
        //lock.release
      }
      
      
      clientSocket.close()
    }
  }
  
  case class clientArrayBufferClass(clientArrayBuffer: ArrayBuffer[Int]) extends Serializable
  case class StringCarrier(string: String, actorIDFrom: Int, actorIDTo: Int) extends Serializable